<!--  -->
<template>
  <div class="">
    <el-card>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item><a href="/">用户管理</a></el-breadcrumb-item>
        <el-breadcrumb-item>用户列表</el-breadcrumb-item>
      </el-breadcrumb>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            v-model.trim="keywords"
            placeholder="请输入搜索关键字"
            clearable
          ></el-input>
        </el-col>
        <el-col :span="8">
          <el-button type="primary" icon="el-icon-search" @click="userSearchFn"
            >搜索</el-button
          >
        </el-col>
      </el-row>
      <el-tabs v-model="activeName" @tab-click="handleTabsClick">
        <el-tab-pane
          :label="tab.title"
          v-for="tab in tabs"
          :key="tab.id"
          :name="tab.name"
        ></el-tab-pane>
      </el-tabs>
      <el-table :data="userListDatas.data" border stripe style="width: 100%">
        <el-table-column prop="uid" label="序号" width="80"> </el-table-column>
        <el-table-column prop="img" label="用户头像" width="80">
          <template slot-scope="scope">
            <img
              :src="'http://localhost:3000' + scope.row.img"
              width="50"
              alt=""
            />
          </template>
        </el-table-column>
        <el-table-column prop="username" label="姓名" width="180">
        </el-table-column>
        <el-table-column prop="level" label="用户等级" width="80">
        </el-table-column>
        <el-table-column prop="age" sortable label="年龄" width="80">
        </el-table-column>
        <el-table-column prop="is_show" label="是否可用" width="180">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.is_show"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-text="开"
              inactive-text="关"
              active-value="1"
              inactive-value="0"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="地址"> </el-table-column>
        <el-table-column fixed="right" label="操作" width="210">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="info" size="small"
              >查看</el-button
            >
            <el-button
              type="primary"
              size="small"
              @click="touseredit(scope.row.uid)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="small"
              @click="deleUserFn(scope.row.uid)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        background
        :page-sizes="[5, 10, 15, 20]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="userListDatas.count"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import { UserListApi, UserDeleApi, UserSearchApi } from "@/api/user.js";
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  //ComponentName和文件名保持一致
  name: "ComponentName",
  data() {
    //这里存放数据
    return {
      currentPage4: 4,
      keywords: "",
      activeName: "0",
      page: 1,
      size: 5,
      level: 0,
      userListDatas: {},
      tabs: [
        {
          id: 0,
          name: "0",
          title: "全部",
        },
        {
          id: 1,
          name: "1",
          title: "管理员",
        },
        {
          id: 2,
          name: "2",
          title: "金牌用户",
        },
        {
          id: 3,
          name: "3",
          title: "银牌用户",
        },
        {
          id: 4,
          name: "4",
          title: "铜牌用户",
        },
      ],
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    handleTabsClick(tab) {
      console.log(tab.index);
      this.level = tab.index;
      this.page = 1;
      this.getUserListDatas();
      // console.log(tab, event);
    },
    handleClick(row) {
      console.log(row);
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.size = val;
      this.page = 1;
      this.getUserListDatas();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val;
      this.getUserListDatas();
    },
    touseredit(uid) {
      // 跳转到修改用户的页面
      this.$router.push({
        name: "useredit",
        path: "/admin/useredit",
        params: {
          uid: uid,
        },
      });
    },
    deleUserFn(uid) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "残忍删除",
        cancelButtonText: "再想想",
        type: "warning",
      })
        .then(() => {
          UserDeleApi({ uid: uid }).then((result) => {
            console.log(result);
            if (result.data.status == 200) {
              this.getUserListDatas();
            } else {
              this.$message({
                type: "warning",
                message: result.data.msg,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async userSearchFn() {
      let result = await UserSearchApi({ keywords: this.keywords });
      console.log(result);
      if (result.data.status == 200) {
        this.userListDatas = result.data;
      }
    },
    async getUserListDatas() {
      let result = await UserListApi({
        page: this.page,
        size: this.size,
        level: this.level,
        keywords: this.keywords,
      });
      console.log(result);
      if (result.data.status == 200) {
        this.userListDatas = result.data;
      }
    },
  },
  beforeCreate() {}, //生命周期 - 创建之前
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  beforeMount() {}, //生命周期 - 挂载之前
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.getUserListDatas();
  },
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
  deactivated() {}, //如果页面有keep-alive缓存功能，当组件被移除时，触发钩子函数deactivated
};
</script>
<style>
</style>